package com.bingo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.bingo.common.R;
import com.bingo.dao.MedicationMapper;

import com.bingo.entity.CurPool;
import com.bingo.entity.Medication;

import com.bingo.entity.dto.everyDayForm.EveryDayMedication;
import com.bingo.entity.dto.resp.MedicationRecord;
import com.bingo.entity.dto.resp.ReportBasic;
import com.bingo.entity.dto.resp.RespSiteMedication;
import com.bingo.service.CurPoolService;
import com.bingo.service.MedicationService;
import com.bingo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Date;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
@Service
public class MedicationServiceImpl extends ServiceImpl<MedicationMapper, Medication> implements MedicationService {
    private CurPoolService curPoolService;
    @Autowired
    public void setPrePoolService(CurPoolService curPoolService) {
        this.curPoolService = curPoolService;
    }

    private UserService userService;
    @Autowired
    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    //新增用药记录
    @Override
    public R add(Medication medication) {

        //查询该塘
        CurPool pool = curPoolService.getCurPoolByNo(medication.getPoolNo());

        if (pool==null){
            return R.error("该塘不存在！");
        }
        if (pool.getType()==0){
            return R.error("该塘是空塘！");
        }
        //查询是否已经存在当天用药记录
        QueryWrapper<Medication> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("pool_no", medication.getPoolNo());
        queryWrapper.eq("date", medication.getDate());
        Medication old = this.getOne(queryWrapper);
        // 获取当前日期
        LocalDate today = LocalDate.now();
        //填充数据
        Medication new_medication = new Medication();
        new_medication.setUserId(medication.getUserId());
        new_medication.setPoolNo(medication.getPoolNo());
        new_medication.setDrug(medication.getDrug());
        new_medication.setManufacturer(medication.getManufacturer());
        new_medication.setDosage(medication.getDosage());
        new_medication.setDate(medication.getDate());
        new_medication.setReportDate(Date.valueOf(today));
        //还未存在当天用药记录
        if (old==null) {
            //保存
            this.save(new_medication);
            return R.success("添加成功");
        }
        else{
            return R.error("存在当天记录！");
        }

    }

    @Override
    public List<EveryDayMedication> selectEveryDayMedication(List<String> poolNos, Date startTime, Date endTime) {
        return null;
    }

    @Override
    public List<RespSiteMedication> selectSiteMedication(List<String> siteNos, Date startTime, Date endTime) {
        return null;
    }

    @Override
    public List<MedicationRecord> selectMedicationRecord(List<String> poolNos, Date startTime, Date endTime, String userId) {
       if (poolNos == null || poolNos.size() == 0){
                poolNos = curPoolService.getPoolNos();
            }

            QueryWrapper<Medication> wrapper = new QueryWrapper<>();
            wrapper.in("pool_no", poolNos);
            wrapper.between("date", startTime, endTime);
            if (userId != null && !userId.equals("")){
                wrapper.eq("user_id", userId);
            }
            List<Medication> list = this.list(wrapper);
            LinkedList<MedicationRecord> medicationRecords = new LinkedList<>();
            HashMap<String, String> allUser = userService.getAllUser();

            for (Medication medication : list) {
                MedicationRecord medicationRecord = new MedicationRecord();
                ReportBasic reportBasic = new ReportBasic();
                reportBasic.setPoolNo(medication.getPoolNo());
                String s = allUser.get(medication.getUserId());
                reportBasic.setReporter(s);
                reportBasic.setDate(medication.getDate());
                reportBasic.setReportDate(medication.getReportDate());
                medicationRecord.setDrug(medication.getDrug());
                medicationRecord.setManufacturer(medication.getManufacturer());
                medicationRecord.setDosage(medication.getDosage());
                medicationRecord.setReportBasic(reportBasic);
                medicationRecords.add(medicationRecord);
            }
            return medicationRecords;
        }

    @Override
    public boolean deleteMedicationRecord(Medication medication) {
        if (medication==null || medication.getPoolNo()==null || medication.getDate()==null ){
            return false;
        }
        QueryWrapper<Medication> wrapper = new QueryWrapper<>();
        wrapper.eq("pool_no", medication.getPoolNo());
        wrapper.eq("date", medication.getDate());
        // 调用remove方法来删除记录
        return this.remove(wrapper);

    }

    @Override
    public boolean update(Medication medication) {
        // 创建更新条件
        UpdateWrapper<Medication> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("pool_no", medication.getPoolNo());
        updateWrapper.eq("date", medication.getDate());

        // 设置需要更新的字段
        updateWrapper.set("drug", medication.getDrug());
        updateWrapper.set("manufacturer", medication.getManufacturer());
        updateWrapper.set("dosage", medication.getDosage());

        // 执行更新操作
        return this.update(updateWrapper);
    }

}
